<?php
namespace app\manage\controller;

use app\model\ShequModel;
use app\manage\controller\Base;
use support\Request;
use think\facade\Db;
use support\View;
class ShequController extends Base
{

    public function index(Request $request)
    {
    	$key=$request->input('key','');
    	View::assign('key',$key);
		$shequM=new ShequModel();
		$shequ=$shequM->where('desc','like','%'.$key.'%')->order('id desc')->paginate(['list_rows'=>10,'query'=>request()->all()])->each(function($item, $key){
			//$item->nickname = 'think';
		});
		View::assign('shequ',$shequ);
		View::assign('count1', $shequ->total());
       	return view('shequ/index');
    }
	public function ajax(Request $request)
    {
    	$data=$request->all();
		$shequ=new ShequModel();
		if($data['type']=='shequ_del'){
			$info=$shequ->destroy($data['id']);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='shequ_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$info=$shequ->saveAll($ar);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='shequ_start'){
		    if($shequ->where('id',$data['id'])->update(['isopen'=>1])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='shequ_stop'){
		    if($shequ->where('id',$data['id'])->update(['isopen'=>0])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		return json(['code' => 0, 'message' => '失败']);
    }
	public function add(Request $request)
    {
    	if(request()->method()=='POST'){
			$data=$request->post();
			$validate = new \app\manage\validate\ShequValidate;
			if(!$validate->check($data)){
				return view_e($validate->getError());
			}	
						$file = $request->file('imgs');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/shequ/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['imgs']=$path;
            }
            
			$shequ=new ShequModel();
			$res=$shequ->save($data);
			if($res){
				return view_s('添加成功',url('shequ/index',['st'=>1]));
			}else{
				return view_e('添加失败');
			}
    	}
		return view('shequ/add');
    }
	public function edit(Request $request)
    {
		$id=$request->input('id',0);
		$shequ = new ShequModel();
    	if(request()->method()=='POST'){
    		$data=$request->post();
			$validate = new \app\manage\validate\ShequValidate;
			if(!$validate->check($data)){
				return view_e($validate->getError());
			}	
						$file = $request->file('imgs');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/shequ/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['imgs']=$path;
            }
            
			$res=$shequ->where('id', $id)->update($data);
			if($res){
				return view_s('修改成功',url('shequ/index',['st'=>1]));
			}else{
				return view_e('修改失败');
			}
    	}
		$data= $shequ->where('id',$id)->find();
		View::assign('data',$data);
		return view('shequ/edit');
    }
}
